# https://www.jfh.com/jfportal/market/index?m=b01
# https://www.jfh.com/jfportal/market/index?m=b01

import requests
import pprint  # 格式化输出
import csv

f = open('外包数据.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=[
    '标题', '地点', '成交时间', '成交内容', '金额'
])
csv_writer.writeheader()  # 写入标题

url = 'https://caigou-api.jfh.com/v1/api/search/main/'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
}
data = {
    'full_query': 'false',
    'graph_id': '1',
    'info_type': 'case',
    'instance_type': 'bottom_hit',
    'keyword': 'python',
    'order': "default",
    'page': '1',
    'page_size': '15'
}
resp = requests.post(url=url, headers=headers, data=data)
result_list = resp.json()['data']['result']
for index in result_list:
    # print(index)
    # pprint.pprint(index)
    dic = {
        '标题': index['case_name'],
        '地点': index['city_name'],
        '成交时间': index['gmt_update'],
        '成交内容': index['object'],
        '金额': index['bid_winning_amount']
        # '供应商': index['package_info']['subcontract_supplier']
    }
    csv_writer.writerow(dic)
    print(dic)
